package net.wofly.common.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.service.IUploadService;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.validator.UploadValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by siqingwei on 2018/3/11.
 */
@RequestMapping("/api/upload")
@RestController
@Api(description = "上传文件接口")
public class UploadController {

    @Autowired
    IUploadService iUploadService;

    /**
     * 上传单个文件
     *
     * @param fileType
     * @param file
     */
    @ApiOperation(value = "上传单个文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileType", value = "文件类型:1:商品图片,2:头像图片,3:签到图片,4:入库单据图片,5:出库单据图片；6：支付单据图片；", required = true, dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(name = "file", value = "单个文件", required = true, dataType = "MultipartFile", paramType = "form")
    })
    @RequestMapping(value = "/uploadSingleFile", method = {RequestMethod.POST})
    public ReturnBean<String> uploadSingleFile(Integer fileType, MultipartFile file, HttpServletRequest request){
        ReturnBean<String> returnBean;
        String errorMessage = UploadValidator.validateUploadSingleFileParams(fileType, file);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            String path = request.getSession().getServletContext().getRealPath("/");
            returnBean = iUploadService.uploadSingleFile(fileType, file, path);
        }
        return returnBean;
    }

    /**
     * 上传单个文件Base64
     *
     * @param fileType
     * @param fileBase64
     */
    @ApiOperation(value = "上传单个文件Base64")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileType", value = "文件类型:1:商品图片,2:头像图片,3:签到图片,4:入库单据图片,5:出库单据图片；6：支付单据图片；", required = true, dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(name = "fileBase64", value = "单个文件Base64", required = true, dataType = "String", paramType = "form")
    })
    @RequestMapping(value = "/uploadSingleFileForBase64", method = {RequestMethod.POST})
    public ReturnBean<String> uploadSingleFileForBase64(Integer fileType, String fileBase64, HttpServletRequest request){
        ReturnBean<String> returnBean;
        String errorMessage = UploadValidator.validateUploadSingleFileForBase64Params(fileType, fileBase64);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            String path = request.getSession().getServletContext().getRealPath("/");
            returnBean = iUploadService.uploadSingleFileForBase64(fileType, fileBase64, path);
        }
        return returnBean;
    }

    /**
     * 上传多个文件
     *
     * @param fileType
     * @param files
     */
    @ApiOperation(value = "上传多个文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileType", value = "文件类型:1:商品图片,2:头像图片,3:签到图片,4:入库单据图片,5:出库单据图片；6：支付单据图片；", required = true, dataType = "Integer", paramType = "form"),
            @ApiImplicitParam(name = "files", value = "多个文件", required = true, dataType = "MultipartFile", paramType = "form")
    })
    @RequestMapping(value = "/uploadBatchFiles", method = {RequestMethod.POST})
    public ReturnBean<List<String>> uploadBatchFiles(Integer fileType, MultipartFile[] files, HttpServletRequest request){
        ReturnBean<List<String>> returnBean;
        String errorMessage = UploadValidator.validateUploadBatchFilesParams(fileType, files);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            String path = request.getSession().getServletContext().getRealPath("/");
            returnBean = iUploadService.uploadBatchFiles(fileType, files, path);
        }
        return returnBean;
    }
}
